package cn.ekuma.epos.analysis.customer.model;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import com.openbravo.bean.erp.viewbean.CustomerAnalysisInfo;
import com.openbravo.bean.erp.viewbean.CustomerTransaction;
import com.openbravo.format.Formats;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.util.IntegerUtils;
import com.openbravo.pos.util.RoundUtils;

public class CustomerAnalysisTableModel extends AbstractTableModel{
    
    List<CustomerAnalysisInfo> rows;
    ArrayList<String> transactionColumnName;

    public CustomerAnalysisTableModel() {
        rows=new ArrayList();
        transactionColumnName=new ArrayList<String>();
    }

    public void resert( ArrayList<String> columns,List<CustomerAnalysisInfo> rows){
        transactionColumnName=columns;
        this.rows=rows;
        this.fireTableStructureChanged();
    }

    public int getRowCount() {
        return rows.size();
    }

    @Override
    public String getColumnName(int column) {
        if(column<COLUMNS_Pref.length)
            return COLUMNS_Pref[column];
         return transactionColumnName.get(column-COLUMNS_Pref.length);
       
    }

    
   private static int[] doubleInt={4,6};
	
    @Override
	public Class<?> getColumnClass(int columnIndex) {
    	 if(columnIndex>=COLUMNS_Pref.length||IntegerUtils.concat(columnIndex, doubleInt))
             return Double.class;
		return super.getColumnClass(columnIndex);
	}

	public int getColumnCount() {
        return COLUMNS_Pref.length+transactionColumnName.size();
    }

    public CustomerAnalysisInfo get(int index){
        return rows.get(index);
    }

    public static String[] COLUMNS_Pref=new String[]{
    	AppLocal.getIntString("label.taxid"),
		AppLocal.getIntString("label.searchkey"),
		AppLocal.getIntString("label.name"),
		"CARD",
		AppLocal.getIntString("label.maxdebt"),
		AppLocal.getIntString("label.curdate"),
		AppLocal.getIntString("label.curdebt")
    };

    public Object getValueAt(int rowIndex, int columnIndex) {
    	CustomerAnalysisInfo selected=rows.get(rowIndex); 
        if(columnIndex<COLUMNS_Pref.length)
        	 switch (columnIndex) {
             case 0:
                 return selected.getTaxid();
             case 1:
                 return selected.getSearchkey();
             case 2:
                 return selected.getName();
             case 3:
                 return selected.getCard();
             case 4:
                 return RoundUtils.round(selected.getMaxdebt());
             case 5:
                 return Formats.TIMESTAMP.formatValue(selected.getCurdate());
             case 6:
                 return RoundUtils.round(selected.getCurdebt());    
        	 }
        if(columnIndex<COLUMNS_Pref.length+transactionColumnName.size()){
        	int realColumnIndex=columnIndex-COLUMNS_Pref.length;
        	CustomerTransaction pTran=selected.getTransaxtion(transactionColumnName.get(realColumnIndex));
            return pTran==null?null:RoundUtils.round(pTran.getTotal());
        }
		return null;
    }
}
